<!doctype html><html lang=cn-zh><head><meta charset=UTF-8><meta name=viewport content="width=device-width,initial-scale=1"><meta http-equiv=X-UA-Compatible content="IE=edge"><style type=text/css>body{font-family:monospace}</style><title>Python-SyntaxError:Non-ASCII character '\e8' in....</title>
<meta name=description content="A blog maintained by Vimiix."><link rel=stylesheet href=/css/style.css><script>var _hmt=_hmt||[];(function(){var e,t=document.createElement("script");t.src="https://hm.baidu.com/hm.js?7c24231917964240bae97e813810620c",e=document.getElementsByTagName("script")[0],e.parentNode.insertBefore(t,e)})()</script></head><body><header>====================<br>== Hi, I'm Vimiix ==<br>====================<div style=float:right;color:gray;font-size:x-large>Get hands dirty.</div><br><p><nav><a href=https://www.vimiix.com/><b>首页</b></a>.
<a href=/posts/><b>文章列表</b></a>.
<a href=/projects/><b>开源项目</b></a>.
<a href=/tags/><b>标签</b></a>.
<a href=/friends/><b>友链</b></a>.
<a href=/about/><b>关于我</b></a>.
<a href=/index.xml><b>RSS</b></a>.</nav></p></header><main><article><h1>Python-SyntaxError:Non-ASCII character '\e8' in....</h1><b><time>2017.03.15 22:53</time></b>
<a href=/tags/python>Python</a>
<a href=/tags/syntaxerror>SyntaxError</a><div><h2 id=syntaxerrornon-ascii-character-e8-in-file>SyntaxError:Non-ASCII character &lsquo;\e8&rsquo; in File&mldr;</h2><p>遇到下面这种提示的错误</p><p><figure><img src=http://vimiix-blog.oss-cn-qingdao.aliyuncs.com/error.png alt></figure></p><p>修正方法：检查文件头部的UTF-8编码语句是否书写正确</p><pre tabindex=0><code>	#coding=utf-8
</code></pre><p>今天下午同事梦鸽遇到一个问题，想直接把要找的一篇网页源代码中的特定的链接找出来，看到她需要每天手动复制粘贴出来然后一点一点删除，这样来找的话，效率也太低了。刚好我最近也刚开始学习python编程，何不写个脚本练练手？</p><p>所以就花了一个多小时帮梦鸽童鞋写了一个自动抓取链接的脚本，虽然开始一直头疼不会正则表达式该怎么写，但最终用笨办法：通过用</p><ul><li>循环</li><li>条件</li><li>逻辑</li></ul><p>这些基本的运算符，帮她实现了这个功能。</p><p>在这个过程中，遇到了最上面的这个错误，排查了半天以为是哪里的标点符号用成了中文字符，最后一想&rsquo;Non-ASCII&rsquo;意思就是找不到对应的ASCII码，既然找不到，是不是文件的编码格式错了，才想起来回头去看文件头部的</p><pre><code>#coding = utf-8
</code></pre><p>这句话，我居然写成了</p><pre><code>#condind=utf-8
</code></pre><p>改正确以后就没问题了。</p><p>很巧的是，同在一起学习python的QQ群同学，晚上也遇到了同样的问题，毫不犹豫的帮他找到了问题所在，顿时感觉半个多小时拍错过程是值得的。</p><p>特记录这篇日记，希望以后不要再出错。</p><h6 id=附python源码抓取网页中csdn博主的文章链接并保存到blogidtxt文件中>附python源码，抓取网页中CSDN博主的文章链接，并保存到blogID.txt文件中</h6><div class=highlight><pre tabindex=0 style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-Python data-lang=Python><span style=display:flex><span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#coding=utf-8</span>
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	<span style=color:#f92672>import</span> re
</span></span><span style=display:flex><span>	
</span></span><span style=display:flex><span>	fp <span style=color:#f92672>=</span> open(<span style=color:#e6db74>&#39;sample.txt&#39;</span>)<span style=color:#75715e>#要打开的文件名</span>
</span></span><span style=display:flex><span>	temp <span style=color:#f92672>=</span> fp<span style=color:#f92672>.</span>read()
</span></span><span style=display:flex><span>	blogID <span style=color:#f92672>=</span> open(<span style=color:#e6db74>&#39;BlogID.txt&#39;</span>, <span style=color:#e6db74>&#39;w&#39;</span>) <span style=color:#75715e>#新建一个文件夹</span>
</span></span><span style=display:flex><span>	tmp_list <span style=color:#f92672>=</span> re<span style=color:#f92672>.</span>findall(<span style=color:#e6db74>r</span><span style=color:#e6db74>&#34;(?&lt;=href=</span><span style=color:#ae81ff>\&#34;</span><span style=color:#e6db74>).+?(?=</span><span style=color:#ae81ff>\&#34;</span><span style=color:#e6db74>)|(?&lt;=href=\&#39;).+?(?=\&#39;)&#34;</span>,temp)<span style=color:#75715e>#检索出页面所有链接</span>
</span></span><span style=display:flex><span>	url_list <span style=color:#f92672>=</span> []
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>for</span> i <span style=color:#f92672>in</span> tmp_list:
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>if</span> (<span style=color:#e6db74>&#39;blogdevteam&#39;</span> <span style=color:#f92672>not</span> <span style=color:#f92672>in</span> i) <span style=color:#f92672>and</span> (<span style=color:#e6db74>&#39;cloumn&#39;</span> <span style=color:#f92672>not</span> <span style=color:#f92672>in</span> i) <span style=color:#f92672>and</span> (<span style=color:#e6db74>&#39;article&#39;</span> <span style=color:#f92672>in</span> i) <span style=color:#f92672>and</span> (<span style=color:#e6db74>&#39;http://blog.csdn.net&#39;</span> <span style=color:#f92672>in</span> i):
</span></span><span style=display:flex><span>		<span style=color:#66d9ef>if</span> i <span style=color:#f92672>not</span> <span style=color:#f92672>in</span> url_list:
</span></span><span style=display:flex><span>			url_list<span style=color:#f92672>.</span>append(i)
</span></span><span style=display:flex><span>	<span style=color:#75715e>#print url_list</span>
</span></span><span style=display:flex><span>	<span style=color:#75715e>#blogID.write(str(url_list))</span>
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>for</span> url <span style=color:#f92672>in</span> url_list:
</span></span><span style=display:flex><span>	<span style=color:#66d9ef>if</span> url <span style=color:#f92672>in</span> url_list:
</span></span><span style=display:flex><span>		print <span style=color:#f92672>&gt;&gt;</span> blogID , url
</span></span><span style=display:flex><span>	blogID<span style=color:#f92672>.</span>close()
</span></span><span style=display:flex><span>	fp<span style=color:#f92672>.</span>close()
</span></span></code></pre></div></div></article></main><aside><div><div><h3>LATEST POSTS</h3></div><div><ul><li><a href=/posts/2025-10-16-kubernetes-apiserver-authorization-mechanism/>Kubernetes APIServer 鉴权机制</a></li><li><a href=/posts/2025-09-30-kubernetes-apiserver-authentication-mechanism/>Kubernetes APIServer 认证机制</a></li><li><a href=/posts/2024-12-16-deploy-kubernetes-by-kubeadm/>使用 kubeadm 搭建 kubernetes 集群</a></li><li><a href=/posts/2024-09-20-how-to-code-review/>如何做code review</a></li><li><a href=/posts/2024-08-12-weakref-in-python/>Python中的弱引用</a></li></ul></div></div></aside><footer><p>Social Links:
<a href=https://github.com/vimiix><b>Github</b></a>.
<a href=https://www.douban.com/people/vimiix/><b>Douban</b></a>.
<a href=mailto:i@vimiix.com><b>Email</b></a>.<br><hr>&copy; 2017-2025
Vimiix Yao; All rights reserved.
<a href=https://beian.miit.gov.cn/>京ICP备19015214号-1</a></p><script src=https://l2dwidget.js.org/lib/L2Dwidget.min.js></script><script>L2Dwidget.init({model:{jsonPath:"https://unpkg.com/live2d-widget-model-tororo@1.0.5/assets/tororo.model.json"}})</script></footer></body></html>